package com.xywl.escort.view.market;

import com.xywl.escort.bean.AddShoppingCartReq;
import com.xywl.escort.bean.BaseResp;
import com.xywl.escort.bean.CommentsReq;
import com.xywl.escort.bean.CommentsResp;
import com.xywl.escort.bean.GoodDetailReq;
import com.xywl.escort.bean.GoodDetailResp;
import com.xywl.escort.bean.SpecResp;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import javax.inject.Inject;

import timber.log.Timber;

/**
 * Created by 32422 on 2018/5/2.
 */

public class ProductDetailsPresenter implements ProductDetailsContract.Presenter {
    private ProductDetailsContract.View mView;
    private ApiServer mApiServer;

    @Inject
    ProductDetailsPresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void takeView(ProductDetailsContract.View view) {
        this.mView = view;
    }

    @Override
    public void loadSpecific(String spuId, String skuId) {
        GoodDetailReq req = new GoodDetailReq();
        req.spuId = spuId;
        req.skuId = skuId;
        mApiServer.goodsSpec(req)
                .compose(RxUtil.<SpecResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<SpecResp>bindToLife())
                .subscribe(new BaseSubscriber<SpecResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.loadSpecificFailure("获取商品规格失败");
                    }

                    @Override
                    public void onNext(SpecResp resp) {
                        if (null != resp) {
                            if (null != resp.data && resp.isOk()){
                                mView.loadSpecificSuccess(resp.data);
                            } else {
                                mView.loadSpecificFailure(resp.msg);
                            }
                        } else {
                            mView.loadSpecificFailure("获取商品规格失败");
                        }
                    }
                });

    }

    @Override
    public void loadProductDetails(String spuId, String skuId) {
        GoodDetailReq req = new GoodDetailReq();
        req.spuId = spuId;
        req.skuId = skuId;
        mApiServer.goodDetail(req)
                .compose(RxUtil.<GoodDetailResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<GoodDetailResp>bindToLife())
                .subscribe(new BaseSubscriber<GoodDetailResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.loadProductDetailsFailure();
                    }

                    @Override
                    public void onNext(GoodDetailResp resp) {
                        if (null != resp && null != resp.data && resp.isOk()) {
                            mView.updateProductDetails(resp.data);
                        } else {
                            mView.loadProductDetailsFailure();
                        }
                    }
                });
    }

    @Override
    public void addShoppingCart(AddShoppingCartReq req) {
        mApiServer.addShoppingCart(req)
                .compose(RxUtil.<BaseResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BaseResp>bindToLife())
                .subscribe(new BaseSubscriber<BaseResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.addShoppingCartFailure("添加宝箱失败");
                    }

                    @Override
                    public void onNext(BaseResp resp) {
                        if (null != resp) {
                            if (resp.isOk()) {
                                mView.addShoppingCartSuccess(resp);
                            } else {
                                mView.addShoppingCartFailure(resp.msg);
                            }
                        } else {
                            mView.addShoppingCartFailure("添加宝箱失败");
                        }
                    }
                });
    }

    @Override
    public void loadComments(CommentsReq req) {
        mApiServer.frontComments(req)
                .compose(RxUtil.<CommentsResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<CommentsResp>bindToLife())
                .subscribe(new BaseSubscriber<CommentsResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.loadCommentsFailure();
                    }

                    @Override
                    public void onNext(CommentsResp resp) {
                        if (null != resp && null != resp.data && resp.isOk()) {
                            mView.loadCommentsSuccess(resp.data);
                        } else {
                            mView.loadCommentsFailure();
                        }
                    }
                });
    }

    @Override
    public void unSubscribe() {
        Timber.tag("ProductDetailsPresenter").e("unSubscribe");
        mView = null;
    }
}
